package basic

import (
	"time"

	"gitee.com/Luna-CY/Golang-Project-Template/entry/http/request"
	"gitee.com/Luna-CY/Golang-Project-Template/entry/http/response"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/context/contextutil"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/errors"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/i18n"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/interface/dao"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/runtime"
	"gitee.com/Luna-CY/Golang-Project-Template/model"
	"github.com/gin-gonic/gin"
)

type SendDelayEmailToRequest struct {
	Email   string   `json:"email" validate:"required" binding:"required,email"`             // 邮件地址
	Subject string   `json:"subject" validate:"required" binding:"required"`                 // 邮件主题
	Content string   `json:"content" validate:"required" binding:"required"`                 // 邮件内容
	Delay   int      `json:"delay" validate:"required" minimum:"1" binding:"required,min=1"` // 延迟时间，单位秒，最小为1
	FileIds []string `json:"file_ids" validate:"optional" binding:"omitempty"`               // 附件的文件ID列表
}

// SendDelayEmailTo 发送延迟邮件
// @tags basic
// @summary 发送延迟邮件
// @param param body SendDelayEmailToRequest true "发送延迟邮件请求"
// @success 200 {object} response.Response{} "发送延迟邮件成功"
// @router /basic/send-delay-email-to [post]
func (cls *Basic) SendDelayEmailTo(c *gin.Context) (response.Code, any, errors.I18nError) {
	var body = SendDelayEmailToRequest{}
	if err := request.ShouldBindJSON(c, &body); nil != err {
		return response.InvalidParams, nil, errors.NewI18n(i18n.CommonIdInvalidRequest, err.Relation(errors.ErrorInvalidRequest("SHGEHB_IC.B_IC.SDET_TO.332130", "参数错误")))
	}

	var ctx = contextutil.NewContextWithGin(c)

	// 生产环境不允许操作
	if runtime.IsProduction() {
		return response.ActionReject, nil, errors.NewI18n(i18n.CommonIdActionReject, errors.New(errors.ErrorTypeActionReject, "SHGWHB_IC.B_IC.SEDT_TO.335830", "不允许的操作"))
	}

	var attachments []*model.SystemObject
	for _, fileId := range body.FileIds {
		attachment, err := cls.storage.GetObjectByCondition(ctx, dao.SystemObjectOptionWithId(fileId))
		if nil != err {
			if err.IsType(errors.ErrorTypeRecordNotFound) {
				return response.InvalidParams, nil, errors.NewI18n(i18n.CommonIdInvalidRequest, err.Relation(errors.ErrorInvalidRequest("SHGEHB_IC.B_IC.SDET_TO.332130", "附件不存在")))
			}

			return response.ServerInternalError, nil, errors.NewI18n(i18n.CommonIdServerInternalError, err.Relation(errors.ErrorServerInternalError("SHGEHB_IC.B_IC.SDET_TO.332130", "读取附件失败")))
		}

		attachments = append(attachments, attachment)
	}

	if err := cls.notice.SendEmailDelayTo(ctx, []string{body.Email}, nil, nil, body.Subject, model.SystemSmtpLogBodyTypeText, body.Content, attachments, time.Duration(body.Delay)*time.Second); nil != err {
		return response.ServerInternalError, nil, errors.NewI18n(i18n.CommonIdServerInternalError, err.Relation(errors.ErrorServerInternalError("SHGEHB_IC.B_IC.SDET_TO.332130", "发送邮件失败")))
	}

	return response.Ok, nil, nil
}
